/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashbox.gui.renders;

import com.cashbox.gui.componentes.CashBoxLabel;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.font.TextLayout;
import java.awt.geom.RoundRectangle2D;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Gustavo Enrique Ruiz Cisneros
 */
public class CashBoxListRender extends javax.swing.JLabel implements ListCellRenderer {
    /*Esta coleccion nos servira para recibir los Iconos identificados
     *con el texto mostrado en la lista */

    HashMap<String, Icon> elementos;
    /*Esta coleccion nos servira para recibir los ToolTip identificados
     *con el texto mostrado en la lista */
    HashMap<String, String> toolTip;

    /*si el elemento de la lista no fue agregado a la coleccion de los iconos
     * se le colocara un icono por defecto */
    ImageIcon icononulo;//= new ImageIcon(this.getClass().getResource("/iconos/error.png"));
    private Color listItemColor = new Color(0, 153, 255);
    private boolean isSelected;
    private float selectionOpacity = 0.3f,
            selectionBorderOpacity = 0.0f,
            listOpacity = 0.00f,
            listBorderOpacity = 0.0f;
    private Color selectionColor = new Color(0, 153, 255);

    /*Constructor recibiendo las listas*/
    public CashBoxListRender(HashMap<String, Icon> pelementos, HashMap<String, String> tool) {
        this.elementos = pelementos;
        toolTip = tool;
    }
    /*Contructor sin Datos*/

    public CashBoxListRender() {
        elementos = new HashMap<String, Icon>();
    }

    @Override
    @SuppressWarnings("element-type-mismatch")
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        /*Primero verificamos si el Texto del Elemento se encuentra
         *dentro de nuestra coleccion*/

        setOpaque(true);
        this.isSelected = isSelected;

        setForeground(listItemColor);
        if (elementos.get(value) != null) {
            //setIcon(elementos.get(value));
            setText(value.toString());

            if (isSelected) {
                //setFont(new Font("Verdana", Font.BOLD, 16));
                //setForeground(Color.white);
                //setBackground(Color.BLUE);
                setOpaque(true);
            } else {
                setFont(null);
                //setForeground(Color.black);
                setOpaque(false);
            }
        } else {
            //setIcon(icononulo);
            setText(value.toString());
            if (isSelected) {
//                setFont(new Font("Verdana", Font.BOLD, 14));
//                setForeground(new java.awt.Color(24, 165, 211));
            } else {
                setFont(null);
                //setForeground(Color.black);
            }
        }

        if (toolTip != null) {
            if (toolTip.get(value) != null) {
                setToolTipText(toolTip.get(value));
            }
        }
        
        return this;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        if (isSelected) {
            paintSelection(g2);
        }
        paintText(g2);
    }

    private void paintSelection(Graphics2D g2) {
        double y = 1.0;
        double height = (double) getHeight() + 50;
        double width = (double) getWidth() + 50;
        RoundRectangle2D background;

        Composite composite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionOpacity));
        g2.setColor(selectionColor);

        background = new RoundRectangle2D.Double(0, y, width, height, 0, 0);

        g2.fill(background);

        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionBorderOpacity));
        Stroke stroke = g2.getStroke();
        g2.setStroke(new BasicStroke(3.0f));
        g2.draw(background);
        g2.setStroke(stroke);

        g2.setComposite(composite);
    }

    private void paintText(Graphics2D g2) {
        FontMetrics fm = getFontMetrics(getFont());
        int x = getInsets().left + 10;
        int y = getInsets().top + fm.getAscent();

        Composite composite = g2.getComposite();

        TextLayout layout = new TextLayout(getText(), getFont(), g2.getFontRenderContext());
        layout.draw(g2, x, y);

        if (isSelected) {
            g2.setComposite(composite);
        }

        g2.setColor(getForeground());
    }
}
